package com.jinyi.wwdeng.utils.dialog

import android.Manifest
import android.content.Context
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.RequiresApi
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions
import com.jinyi.base.utils.log.HHLog
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.DialogPhotoBinding
import com.jinyi.wwdeng.utils.event.TrackEventType
import com.jinyi.wwdeng.utils.event.TrackEventUtils

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 拍照、相册选择 dialog
 */
class PhotoDialog: ProDialog {
    private lateinit var binding: DialogPhotoBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogPhotoBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.ivClose) //关闭
        setOnClickListener(R.id.cameraLayout) //拍照
        setOnClickListener(R.id.albumLayout) //相册选择
    }

    override fun initData() {
    }

    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onViewClick(v: View?) {
        dismiss()
        when(v!!.id){
            R.id.cameraLayout -> {
                val permissions = arrayListOf(
                    Manifest.permission.CAMERA
                )
                if (XXPermissions.isGranted(context,permissions)){
                    if(mCallback != null){
                        mCallback!!.openCamera()
                    }
                } else {
                    XXPermissions.with(context)
                        // 申请多个权限
                        .permission(permissions)
                        .request(object: OnPermissionCallback {
                            override fun onGranted(p0: MutableList<String>, p1: Boolean) {
                                var permissionString = ""
                                p0.forEach {
                                    permissionString = "$permissionString$it,"
                                }
                                TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_PERMISSION_SYSTEM_AGREE,permissionString.substring(0,permissionString.length - 1))
                                if(mCallback != null){
                                    mCallback!!.openCamera()
                                }
                            }
                            override fun onDenied(p0: MutableList<String>, doNotAskAgain: Boolean) {
                                var permissionString = ""
                                p0.forEach {
                                    permissionString = "$permissionString$it,"
                                }
                                TrackEventUtils.logEvent(TrackEventType.CLICK.CLICK_PERMISSION_SYSTEM_DENY,permissionString.substring(0,permissionString.length - 1))
                                XXPermissions.startPermissionActivity(context,permissions)
                            }
                        })
                }
            }
            R.id.albumLayout -> {
                if(mCallback != null){
                    mCallback!!.openAlbum()
                }
            }
        }
    }

    interface IDialogCallback {
        //打开相机
        fun openCamera()
        //打开相册
        fun openAlbum()
    }

    override fun onStart() {
        super.onStart()
        TrackEventUtils.logEvent(TrackEventType.DIALOG.DIALOG_PHOTO_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        TrackEventUtils.logEvent(TrackEventType.DIALOG.DIALOG_PHOTO_DISMISS,eventRemark)
    }
}